package CommitLink;

import gitobject.Commit;

import javax.xml.transform.Source;
import java.io.Serializable;
import java.util.ArrayList;


/**
 * @author shkstart
 * @create 2021-12-18 17:02
 */
public class LinkList implements Serializable {
    private static final Long serialVersionUID = 3455710433843546891l;
    //头结点
    private LNode first;
    //尾结点
    private LNode last;

    public LinkList(){
        first = null;
        last = null;
    }
    public static class LNode implements Serializable{
        //序列号
        private static final Long serialVersionUID = 3455710433843546891l;
        //结点存储的commit对象
        private Commit commit;
        //前驱数组
        private ArrayList<LNode> pre;
        //后继数组
        private ArrayList<LNode> next;

        public LNode() {
        }

        public LNode(Commit commit) {
            this.commit = commit;
            pre = new ArrayList<>();
            next = new ArrayList<>();
        }

        public Commit getCommit() {
            return commit;
        }

        public void setCommit(Commit commit) {
            this.commit = commit;
        }

        public ArrayList<LNode> getPre() {
            return pre;
        }

        public ArrayList<LNode> getNext() {
            return pre;
        }

        public void addPre(LNode p) {
            this.pre.add(p);
        }

    }





        /**
         * 插入数据
         * @param value
         */
        public void insertLast(Commit value){

            LNode temp = last;
            LNode newLNode = new LNode(value);
            newLNode.pre.add(last);
            if(temp != null) {
                temp.next.add(newLNode);
            }
            if(first == null){
                first = newLNode;
            }
            //把最后个节点设置为最新的节点
            last = newLNode;
        }

        public void switchLast(String commitId) throws Exception {
            LNode temp = findByValue(commitId);
            last = temp;
        }


        public boolean isEmpty(){
            return last == null;
        }


        /**
         * 删除头节点时要去除两个指针,一个指向下个的next ,一个是next的pre指向前面的
         *
         * @param
         * @return
         */
        public LNode deleteLast(){

            LNode temp = last;
            last = null;
            //只有一个父节点时
            last = temp.pre.get(0);
            return temp;
        }

        /**
         * 删除
         * @param value
         * @return
         */
//        public LNode deleteByKey(Commit value){
//            LNode temp = last;
//            LNode current = last;
//            while(current.commit != value){
//                if (current.pre == null) {
//                    System.out.println("没找到节点");
//                    return null;
//                }
//                current = current.next;
//            }
//            if (current == first) {
//                //return deleteFirst();
//                //指向下个就表示删除第一个
//                first = first.next;
//            }else {
//                current.pre.next = current.next;
//                current.next.pre = current.pre;
//            }
//            return current;
//        }

        /**
         * 显示所有的数据
         */
        public void display(){
            if (last == null) {
                return;
            }
            dfs(last);
        }

        public void dfs(LNode temp){
            if(temp == null){
                return;
            }
            System.out.println(temp.getCommit());
            System.out.println("--------------------------------");
            for(LNode tem: temp.pre){
                dfs(tem);
            }
        }

        /**
         * 查找节点1
         * @param commitId
         * @return
         */
        public LNode findByValue(String commitId) throws Exception {
            if (first == null){
                return null;
            }
            LNode temp = first;
            LNode re = dfsFind(first,commitId);
            first = temp;
            return re;
        }

        LNode res;
        public LNode dfsFind(LNode lNode, String commitId) throws Exception {
            if(lNode == null){
                return null;
            }
            if(lNode.getCommit().genKey().equals(commitId) ){
                res = lNode;
                return res;
            }
            for(LNode tem: lNode.next){
                LNode lNode1 = dfsFind(tem, commitId);
            }
            return res;
        }
//        /**
//         * 根据索引查找对应的值
//         * @param position
//         * @return
//         */
//        public LNode findByPosition(int position){
//            if(position > getLength() - 1 || position < 0){
//                System.out.println("数组越界");
//                return null;
//            }
//            LNode current = first;
//            //为什么是position - 1，因为要使用遍历，让current指向下一个， 所以position - 1的下个LNode就是要找的值
//            for (int i = 0; i <= position - 1 ; i++) {
//                current  = current.next;
//            }
//            return current;
//        }

    /**
     * 获取链表长度
     *
     * @param
     * @return
     */
//    public int getLength() {
//        LNode current = first;
//        int length = 0;
//        while (current != null) {
//            current = current.next;
//            length++;
//        }
//        return length;
//    }

    public Commit getLast(){
        if(last == null){
            return null;
        }
        return last.commit;
    }

}
